package com.ytc.position.controller;

import com.ytc.data.model.Data;
import com.ytc.data.service.DataService;
import com.ytc.position.model.Position;
import com.ytc.position.service.PositionService;
import com.ytc.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//岗位发布
@Controller
@RequestMapping("position")
public class PositionController {

    @Autowired
    private PositionService positionService;

    @Autowired
    private DataService dataService;

    //跳转到新增页面
    @RequestMapping("toAddPosition")
    public String toAddPosition(Model model){

        List<Data> degree = dataService.getData("学历要求");
        List<Data> experience = dataService.getData("工作经验");
        List<Data> salary = dataService.getData("薪资待遇");

        model.addAttribute("degree",degree);
        model.addAttribute("experience",experience);
        model.addAttribute("salary",salary);
        return "position/addPosition";
    }

    //跳转到列表页面
    @RequestMapping("position")
    public String position(Model model,HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        List<Position> list = positionService.getPosition(user.getUserId());

        model.addAttribute("position",list);

        return "position/position";
    }

    //跳转到修改页面
    @RequestMapping("toUpdatePosition")
    public String toUpdatePosition(Model model,Integer positionId){
        Position position = positionService.get(positionId);
        List<Data> degree = dataService.getData("学历要求");
        List<Data> experience = dataService.getData("工作经验");
        List<Data> salary = dataService.getData("薪资待遇");

        model.addAttribute("position",position);
        model.addAttribute("degree",degree);
        model.addAttribute("experience",experience);
        model.addAttribute("salary",salary);
        return "position/updatePosition";
    }

    //新增
    @RequestMapping("addPosition")
    @ResponseBody
    public void addPosition(Position position, HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        position.setUserId(user.getUserId());
        positionService.addPosition(position);
    }

    //获取
    @RequestMapping("getPosition")
    @ResponseBody
    public List<Position> getPosition(HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        List<Position> list = positionService.getPosition(user.getUserId());
        return list;

    }

    //修改
    @RequestMapping("updatePosition")
    @ResponseBody
    public void updatePosition(Position position){
        positionService.updatePosition(position);
    }

    //删除
    @RequestMapping("deletePosition")
    @ResponseBody
    public void deletePosition(Integer positionId){
        positionService.deletePosition(positionId);
    }


    //跳转到修改页面
    @RequestMapping("toPosition")
    public String toUpdatePosition(Model model,HttpServletRequest request){
        User user =(User) request.getSession().getAttribute("user");
        List<Position> list = positionService.getPosition(user.getUserId());
        model.addAttribute("position",list);
        return "position/position";
    }

}
